<template>
	<view>
		<swiper class="screen-swiper" :class="dotStyle?'square-dot':'round-dot'" :indicator-dots="true" :circular="true"
		 :autoplay="true" interval="5000" duration="500">
			<swiper-item v-for="(item,index) in swiperList" :key="index">
				<image :src="item.url" mode="aspectFill" v-if="item.type=='image'"></image>
				<video :src="item.url" autoplay loop muted :show-play-btn="false" :controls="false" objectFit="cover" v-if="item.type=='video'"></video>
			</swiper-item>
		</swiper>

		<!-- 导航区域 -->
		<view v-for="(menu,indexMenu) in ButtonsMenu" :key="indexMenu">
			<view class="cu-bar bg-white solid-bottom margin-top" >
				<view class="action">
					<text class="cuIcon-title text-orange "></text>{{menu.titleBlock}}
				</view>
			</view>
			<view class="cu-list grid" :class="['col-' + gridCol,gridBorder?'':'no-border']" :style="indexMenu==ButtonsMenu.length-1?'padding-bottom: 50px;':''">
				<view class="cu-item" v-for="(item,index) in menu.buttons" :key="index" v-if="!authcheck || item.display">
					<navigator :url="item.path">
						<view :class="['cuIcon-' + item.icon,'text-' + (item.color||'blue')]" style="font-size:30px;">
							<view class="cu-tag badge" v-if="item.badge!=0 && item.badge!=null">
								<block v-if="item.badge!=1">{{item.badge>99?'99+':item.badge}}</block>
							</view>
						</view>
						<text>{{item.text}}</text>
					</navigator>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import formatData from '@/common/formData.js';
	export default {
		data() {
			return {
				cardCur: 0,
				swiperList:[{
					id: 0,
					type: 'image',
					url: '/static/sample.jpg'
				}],
				authcheck:this.test ? false : true,//是否需要验证菜单权限
				dotStyle: true,
				towerStart: 0,
				keyMenu: 'sample.menu.uniapp.eqinfo',
				direction: '',
				gridCol: 3,
				gridBorder: false,
				testData:'',
				openSetting:false,
				ButtonsMenuBak:[],
				//仓库菜单
				ButtonsMenu:[
					{
						titleBlock:"样品操作",
						buttons:[
							{text:"新增入库",icon:"down",path:"/pages/sample/rukuForm",'itemId':'ruku'},
							{text:"入库列表",icon:"favor",path:"/pages/sample/rukuList",'itemId':'rukuList'},
							{text:"扫码找样",icon:"scan",path:"/pages/sample/searchSample",'itemId':'search'},
							{text:"新增出库",icon:"pick",path:"/pages/sample/chukuForm",'itemId':'chuku'},
							{text:"出库列表",icon:"ticket",path:"/pages/sample/chukuList",'itemId':'chukuList'},
							{text:"库存调整",icon:"search",path:"/pages/sample/diaoKu",'itemId':'diaoKu'},
						],
					},
					{
						titleBlock:"基础档案",
						buttons:[
							{text:"客户列表",icon:"peoplelist",path:"/pages/material/purchaseRuku",'itemId':'client'},
							{text:"样品列表",icon:"tag",path:"/pages/material/product",'itemId':'product'},
							{text:"样品货位",icon:"similar",path:"/pages/material/purchaseRukuList",'itemId':'kuwei'},
						],
					}
				],
			};
		},
		//组件的生命周期：created，beforeCreate，beforeMount，mounted，beforeUpdate，updated,beforeDestroy,destroyed
		onLoad() {			
			// 在colorui 的个性tabbar中，这个组件页面的onload是不会触发的，请勿掉坑		
			// onshow也不会触发
		},
		created(){
			
		},
		beforeMount(){
			console.log('beforeMount home');			
			var _this = this;
			//先加载缓存中的菜单：：uni.getSetting会有点延迟，所以在里面会导致菜单加载缓慢下
			_this.cacheGetMenu();
			
			// #ifdef MP-WEIXIN
			var getUserInfo = uni.canIUse('button.open-type.getUserInfo');
			if(getUserInfo){
				uni.getSetting({
				    success(res) {
						if(!res.authSetting['scope.userInfo']){
							console.log('authorize fail',res.authSetting);
							uni.navigateTo({
							    url: '/pages/weixin/authSetting'
							});
						}else{
							_this.initHome();
						}
				    }
				});
			}else{
				_this.initHome();
			}
			// #endif
			
			// #ifndef MP-WEIXIN
			_this.initHome();
			// #endif
		},
		mounted() {
			
		},
		onShareAppMessage(res) {
		    return {
		      title: '易奇样品间',
		      path: '/pages/index/index'
		    }
		},
		methods: {
			initHome(){
				console.log('initHome home');
				//初始化home页面的方法集中处理下
				var _this = this;
				formatData.getUserinfo(function(user){
					let {errcode,msg} = user;
					console.log(user);
					if(!user.userId){						
						uni.showToast({
							icon:'none',
							title:"无用户信息",
							duration:2500,
							mask:true
						});
						//如果没有有效的userid登录信息,则跳转提示需要登录						
						setTimeout(function(){
							uni.reLaunch({
								url: '/pages/login/login'
							});					
						},1000)
					}else{
						//获取权限菜单，仅显示有权限的菜单项目
						_this.getSwiperList();
						//更新服务器的菜单
						_this.getMenu();
					}
				});
			},
			cacheGetMenu: function(){
				var _this = this;				
				var km = uni.getStorageSync(_this.keyMenu);
				if(km){
					km = JSON.parse(km);
					if(km){
						_this.ButtonsMenuBak = _this.ButtonsMenu;
						_this.ButtonsMenu = km;
					}
				}
			},
			getMenu: function() {
				// console.log('getMenu');
				//获取菜单并处理显示哪些菜单
				var _this = this;
				var buttonMenu = _this.ButtonsMenuBak.length > 0 ? _this.ButtonsMenuBak : _this.ButtonsMenu;
				_this.ButtonsMenuBak = [];
				
				//定制轮播图的时候用
				var params = {
					method:'menu.list.get',
				};
				formatData.set(params);
				uni.request({
					url: formatData.httpUrl(), //仅为示例，并非真实接口地址。
					data:params,
					success: (res) => {
						var result = res.data.data;
						if(result.menuId){
							for(var k in buttonMenu){
								var itemM = buttonMenu[k];
								for(var i in itemM.buttons){
									var item = itemM.buttons[i];
									buttonMenu[k].buttons[i].display = false;
									if(result.menuId.indexOf(item.itemId) >= 0){
										buttonMenu[k].buttons[i].display = true;
									}									
								}
								this.$set(_this.ButtonsMenu ,k ,buttonMenu[k]);
							}
						}
						// _this.ButtonsMenu = buttonMenu;
						// console.log('_this.ButtonsMenu',buttonMenu);
						//把菜单数据缓存到本地缓存中
						uni.setStorageSync(_this.keyMenu,JSON.stringify(_this.ButtonsMenu));
					}
				});
			},
			getSwiperList: function(){
				// console.log('getSwiperList');
				var _this = this;
				//定制轮播图的时候用
				var params = {
					method:'uni.getSwiperImages',
					platfrom: 'weixin',
				};
				formatData.set(params);
				uni.request({
					url: formatData.httpUrl(), //仅为示例，并非真实接口地址。
					data:params,
					success: (res) => {
						var result = res.data.data;
						if(result.swiperList.length > 0){
							_this.swiperList = result.swiperList;
						}
					}
				});
			}
		},
	}
</script>

<style>
	.tower-swiper .tower-item {
		transform: scale(calc(0.5 + var(--index) / 10));
		margin-left: calc(var(--left) * 100upx - 150upx);
		z-index: var(--index);
	}
</style>
